#include "chatdatapack.h"

const QByteArray ChatDataPack::ChatterHeader = QByteArray::fromHex("5555AAAA");
const QByteArray ChatDataPack::ChatterTail = QByteArray::fromHex("55AA55AA");

ChatDataPack::ChatDataPack() {}

ChatDataPack &ChatDataPack::operator<<(const QByteArray &data) {
  buf.append(data);
  return *this;
}

ChatDataPack &ChatDataPack::operator>>(QByteArray &out) {
  out = std::move(data_buf);
  data_buf.clear();
  return *this;
}
QByteArray ChatDataPack::pack(const QByteArray &data) {
  return ChatterHeader + data + ChatterTail;
}

ChatDataPack::operator bool() const {
  if (!data_buf.isEmpty())
    return true;
  int iH = buf.indexOf(ChatterHeader);
  if (iH == -1)
    return false;
  int iT = buf.indexOf(ChatterTail, iH + 1);
  if (iT == -1)
    return false;
  data_buf = buf.mid(iH + ChatterHeader.size(), iT - iH - ChatterHeader.size());
  buf.remove(0, iT + ChatterTail.size());
  return true;
}
